package com.clz.hyperit.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.clz.hyperit.dao.NoteDao;
import com.clz.hyperit.entity.Article;
import com.clz.hyperit.entity.Note;
import com.clz.hyperit.vo.PageInfo;

@Repository("noteDao")
public class NoteDaoImpl extends BaseDaoImpl<Note> implements NoteDao {

	@SuppressWarnings("unchecked")
	public List<Note> findPersonNote(Integer userdetail_id, Integer status,PageInfo pageInfo) {
		String hql = "from Note where status = :status and userdetail_id = :userdetail_id order by createtime desc";
		List<Note> list = getCurrentSession()
				.createQuery(hql)
				.setInteger("status", status)
				.setInteger("userdetail_id", userdetail_id)
				.setFirstResult((pageInfo.getIndexPageNum() - 1) * pageInfo.getSize())
				.setMaxResults(pageInfo.getSize()).list();
		// 获得表中总的条目数目
		String hql2 = "select count(*) from Note where status = :status and userdetail_id = :userdetail_id ";
		Object uniqueResult = getCurrentSession().createQuery(hql2)
				.setInteger("status", status)
				.setInteger("userdetail_id", userdetail_id)
				.uniqueResult();
		Long intValue = (new Integer(uniqueResult.toString())).longValue();
		pageInfo.setTotalCount(intValue);
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<Note> findNoteByRead(PageInfo pageInfo,Integer status) {
		String hql = "from Note where status = :status order by reading desc";
		List<Note> list = getCurrentSession()
				.createQuery(hql)
				.setInteger("status", status)
				.setFirstResult((pageInfo.getIndexPageNum() - 1) * pageInfo.getSize())
				.setMaxResults(pageInfo.getSize()).list();
		// 获得表中总的条目数目
		String hql2 = "select count(*) from Note where status = :status";
		Object uniqueResult = getCurrentSession().createQuery(hql2)
				.setInteger("status", status)
				.uniqueResult();
		Long intValue = (new Integer(uniqueResult.toString())).longValue();
		pageInfo.setTotalCount(intValue);
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<Note> findNoteByCollect(PageInfo pageInfo, Integer status) {
		String hql = "from Note where status = :status order by collecting desc";
		List<Note> list = getCurrentSession()
				.createQuery(hql)
				.setInteger("status", status)
				.setFirstResult((pageInfo.getIndexPageNum() - 1) * pageInfo.getSize())
				.setMaxResults(pageInfo.getSize()).list();
		// 获得表中总的条目数目
		String hql2 = "select count(*) from Note where status = :status";
		Object uniqueResult = getCurrentSession().createQuery(hql2)
				.setInteger("status", status)
				.uniqueResult();
		Long intValue = (new Integer(uniqueResult.toString())).longValue();
		pageInfo.setTotalCount(intValue);
		return list;
	}

	public List<Note> findNoteBysearch(PageInfo pageInfo, String search,Integer status) {
		String hql = "from Note where status = :status and title like :search order by createtime desc";
		@SuppressWarnings("unchecked")
		List<Note> list = getCurrentSession()
				.createQuery(hql)
				.setInteger("status",status)
				.setString("search","%"+search+"%")
				.setFirstResult((pageInfo.getIndexPageNum() - 1) * pageInfo.getSize())
				.setMaxResults(pageInfo.getSize()).list();
		// 获得表中总的条目数目
		String hql2 = "select count(*) from Note where status = :status and title like :search ";
		Object uniqueResult = getCurrentSession().createQuery(hql2)
				.setString("search","%"+search+"%").setInteger("status",status)
				.uniqueResult();
		Long intValue = (new Integer(uniqueResult.toString())).longValue();
		pageInfo.setTotalCount(intValue);
		return list;
	}
}
